<?php 
	namespace Home\Controller;
	use Think\Controller;

	class ProgramController extends AuthController
	{
		public function index(){return;}
		// 登陆用户对应省份节目展示
		public function all()
		{
			if(IS_POST){
				$userid = session("user_id");
				$sort = I("post.sort","upload_time");
				$order = I("post.order","desc");
				$page = I("post.page",1);
				$rows = I("post.rows",25);
				// 搜索参数				
				$program_name = trim(I("post.program_name",''));
				$province_name = trim(I("post.province_name",''));
				// 筛选判断   超管/普通用户 分别调接口
				$userFlag = $this->get_userflag($userid);
				// if($userFlag == 1){
				$res = $this->get_programs($page,$rows,$sort,$order,$program_name,$province_name);
				$output['result'] = $this->getState('7');
				$output['total'] = $res['total']; 
				$output['rows'] = $res['res']; 				
				echo json_encode($output);
				// }else{
				// 	$res = D("UserProvince")->get_province($userid,$page,$rows,$sort,$order,$program_name,$province_name);
				// 	// dump($res);exit();
				// 	$output['result'] = $this->getState('7');
				// 	$output['total'] = $res['total']; 
				// 	$output['rows'] = $res['res']; 				
				// 	echo json_encode($output);
				// }			
			}else{

			}			
		}

		// 获取一条节目详情
		public function detail()
		{
			if(IS_POST){
				$userid = session("user_id");
				$id = I("post.id","");
				// $res = D("UserProvince")->get_province($userid,$page="",$rows="",$sort="upload_time",$order="desc",$program_name="",$province_name="");	
				// 根据剧头id查出对应的剧头
				$res = D("Program")->where(["id"=>$id])->select();
				// dump($res);exit();   	
				if($id != ""){
					foreach ($res as $key => $value) {
						if($value['id'] == $id){
							$res1 = $res[$key];
						}
					}
					// 查出对应节目图片
					$res_pic = D("Picture")->get_pic($id);
					$res1['address'] = $res_pic;
					// dump($res1);exit();
				}else{
					return $res1 = [];
				}		
				echo json_encode($res1);
			}else{

			}
		}

		/**
		 * 修改节目所属省份及上下线状态
		 */
		public function edit()
		{
			if(IS_POST){
				$id = I("post.id","");
				$province = I("post.province","");
				$status = I("post.status","");
				$result = D("Program")->edit($id,$province,$status);
				echo json_encode($result);
			}else{

			}
		}

		/**
		 * 分开查询超管-->全部节目   普通用户从原接口查
		 * @return [type] [description]
		 */
		public function edit_select()
		{
			if(IS_POST){
				$userid = session("user_id");
				$role_id = D("SysUser")->where(["id"=>$userid])->getField("role_id");
				$aa = A("Auth")->getPermiss($role_id);
				if($aa){
					$program_id = I("post.id","");
					$userid = session("user_id");
					$userFlag = $this->get_userflag($userid);
					$page = I("post.page","");
					$rows = I("post.rows","");
					// 获取所有的节目
					$res = $this->get_programs($page,$rows,$sort="upload_time",$order="desc",$program_name="",$province_name="");
					if($userFlag == 1){
						$sql = "SELECT province from t_province";
					}else{
						$sql = "SELECT a.province from t_province as a JOIN user_province as b on b.province_id = a.id WHERE b.user_id = {$userid}";
					}
					$resp = M()->query($sql);
					$resp = implode(",",array_column($resp, "province"));
					if($program_id != ""){
						foreach ($res['res'] as $key => $value) {
							if($value['id'] == $program_id){
								$res['res'][$key]['province_name1'] = $resp;
								$res1 = $res['res'][$key];
							}
						}
						echo json_encode($res1);
					}else{
						return;
					}
				}else{
					return;
				}
			}else{
				return;
			}
		}

		public function all_province()
		{
			if(IS_POST){
				$userid = session("user_id");
				$role_id = D("SysUser")->where(["id"=>$userid])->getField("role_id");
				// if($role_id == 1){   //超管
				$page = I("post.page",1);
				$rows = I("post.rows",10);
				$res = D("Province")->get_all_province($page,$rows);
				// $output['result'] = $this->getState("9");
				// $output['total'] = $res['total']; 
				$output = $res['rows']; 
				foreach ($output as $key => $value) {
					$output[$key]['text'] = $output[$key]['province'];
				}
				// }else{
				// 	//非超管  根据用户id进行
				// 	$Model = new \Think\Model();
				// 	$sql = "SELECT a.* from t_province as a LEFT JOIN user_province as b ON a.id = b.province_id WHERE b.user_id = {$userid};";
				// 	$res = $Model->query($sql);
				// 	foreach ($res as $key => $value) {
				// 		$res[$key]['text'] = $res[$key]['province'];
				// 	}
				// 	$output = $res;
				// }
				// dump($output);exit();
				echo json_encode($output);
			}else{
				// $page = I("post.page",1);
				// $rows = I("post.rows",10);
				// $res = D("Province")->get_all_province($page,$rows);
				// // $output['result'] = $this->getState("9");
				// // $output['total'] = $res['total']; 
				// $output = $res['rows']; 
				// foreach ($output as $key => $value) {
				// 	$output[$key]['text'] = $output[$key]['province'];
				// }
				// // var_dump($output['rows']);exit;
				// echo json_encode($output);
			}
		}

		public function seriesData()
		{
			if(IS_POST){
				// 获取到剧头对应子集
				$program_id = I("post.program_id","272");
				$programs = D("Program")->where(["parent_id"=>$program_id])->select();
				dump($programs);exit();
			}else{

			}
		}

		public function get_userflag($userid)
		{
			if($userid){
				$role_id = D("SysUser")->where(["id"=>$userid])->getField("role_id");
				return $role_id;
			}else{
				return ;
			}
		}

		/*
		 *  超管对应=>所有节目
		 */ 
		public function get_programs($page,$rows,$sort,$order,$program_name,$province_name)
		{	
			$program_name && $map['program_name'] = array("like","%".$program_name."%");
			$map['type'] = array("in","1,2");
			// $province_id = D("Province")->where(["province"=>$province_name])->field("id")->select();
			// 存取省份id
			// $programs = D("Program")->where($map)->order(array($sort=>$order))->page($page,$rows)->select();
			$Model = new \Think\Model();
			$first = ($page-1)*$rows;
			if($province_name != "" && $province_name){
				$sql = "select a.*,group_concat(c.province) as province_name from t_program as a 
					left join program_province as b on a.id = b.program_id 
					left join t_province as c on b.province_id = c.id 
					where a.type < 3 and a.program_name like '%$program_name%' and c.province like '%$province_name%'
					group by a.id 
					order by upload_time desc
					limit $first,$rows;";
			}elseif($rows == "" && $page == ""){
				$sql = "select a.*,group_concat(c.province) as province_name from t_program as a 
					left join program_province as b on a.id = b.program_id 
					left join t_province as c on b.province_id = c.id 
					where a.type < 3 and a.program_name like '%$program_name%' 
					group by a.id 
					order by upload_time desc";
			}else{
				$sql = "select a.*,group_concat(c.province) as province_name from t_program as a 
					left join program_province as b on a.id = b.program_id 
					left join t_province as c on b.province_id = c.id 
					where a.type < 3 and a.program_name like '%$program_name%' 
					group by a.id 
					order by upload_time desc
					limit $first,$rows;";
			}
   			$programs = $Model->query($sql);
			$all_programs = D("Program")->where($map)->order(array($sort=>$order))->select();
			$result['total'] = count($all_programs);
			$result['res'] = $programs;
			return $result;
		}

		// 剧集detail页面
		public function get_seriers()
		{
			$program_id = I("post.program_id","");
			// 根据剧集id查出对应码率的视频文件
			if($program_id){
			    $res = D("Program")->where(["parent_id"=>$program_id])->order('episode')->select();
				$total = count($res);
				$output['total'] = $total;
				$output['rows'] = $res;

			}else{
				$output = ["error"=>"参数缺失"];
			}
			echo json_encode($output);	
		}

		/**
		 * 弃用码率剧集入库
		 * @return [type] [description]
		 */	
		public function changeuse()
		{
			$userid = session("user_id");
			$program_id = I("post.program_id","");
			$roleflag = $this->get_role($userid);
			if(!$program_id){
				$output["message"] = "参数缺失";
			}else{
				if($roleflag == 1){
					$output['message'] = "超管不弃用";
				}else{			
					$map['province_id']  = array('neq',1);	
					$map['user_id'] = $userid;
					$proid = D("UserProvince")->field("province_id")->where($map)->find();
					$province_id = $proid['province_id'];
					$type = D("Province")->field("type")->where(["id"=>$province_id])->find();
					$arr = explode(",", $type['type']);  //码率数组  $proid['province_id']  省份id
					foreach ($arr as $key => $value) {
						$where["code_rate"] = $value;
						$where["program_id"] = $program_id;
						$movie_ids = D("Movie")->field("id")->where($where)->select();
						foreach ($movie_ids as $k => $v) {
							$movie_id = $v['id'];
							$data['province_id'] = $province_id;
							$data['movie_id'] = $movie_id;
							$where1['province_id'] = $province_id;
							$where1['movie_id'] = $movie_id;
							$has = D("TDownload")->where($where1)->select();
							if(!$has){
								$data['create_time'] = time();
								$data['update_time'] = time();
								$data['download_status'] = -3;
								$res = D("TDownload")->data($data)->add();
								$res?$output['message'] = "节目弃用成功":$output['message'] = "节目弃用失败";
							}else{
								$output['message'] = "节目已弃用";
							}
							
						}
					}
				}			
			}	
			echo $output['message'];
		}

		public function get_role($userid)
		{
			$role_id = D("SysUser")->field("role_id")->where(["id"=>$userid])->find();
			if(!empty($role_id)){
			 	return $role_id['role_id'];
			}else{
				return;
			}
		}

	}
 ?>